package com.itboy.mall.ums.member.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.itboy.common.annotation.RepeatSubmit;
import com.itboy.common.annotation.Log;
import com.itboy.common.core.controller.BaseController;
import com.itboy.common.core.domain.AjaxResult;
import com.itboy.common.enums.BusinessType;
import com.itboy.mall.ums.member.vo.MallMemberVo;
import com.itboy.mall.ums.member.bo.MallMemberQueryBo;
import com.itboy.mall.ums.member.bo.MallMemberAddBo;
import com.itboy.mall.ums.member.bo.MallMemberEditBo;
import com.itboy.mall.ums.member.service.IMallMemberService;
import com.itboy.common.utils.poi.ExcelUtil;
import com.itboy.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 会员管理Controller
 *
 * @author itboy
 * @date 2021-09-21
 */
@Validated
@Api(value = "会员管理控制器", tags = {"会员管理管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/member/member")
public class MallMemberController extends BaseController {

    private final IMallMemberService iMallMemberService;

    /**
     * 查询会员管理列表
     */
    @ApiOperation("查询会员管理列表")
    @PreAuthorize("@ss.hasPermi('member:member:list')")
    @GetMapping("/list")
    public TableDataInfo<MallMemberVo> list(@Validated MallMemberQueryBo bo) {
        return iMallMemberService.queryPageList(bo);
    }

    /**
     * 导出会员管理列表
     */
    @ApiOperation("导出会员管理列表")
    @PreAuthorize("@ss.hasPermi('member:member:export')")
    @Log(title = "会员管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<MallMemberVo> export(@Validated MallMemberQueryBo bo) {
        List<MallMemberVo> list = iMallMemberService.queryList(bo);
        ExcelUtil<MallMemberVo> util = new ExcelUtil<MallMemberVo>(MallMemberVo.class);
        return util.exportExcel(list, "会员管理");
    }

    /**
     * 获取会员管理详细信息
     */
    @ApiOperation("获取会员管理详细信息")
    @PreAuthorize("@ss.hasPermi('member:member:query')")
    @GetMapping("/{id}")
    public AjaxResult<MallMemberVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iMallMemberService.queryById(id));
    }

    /**
     * 新增会员管理
     */
    @ApiOperation("新增会员管理")
    @PreAuthorize("@ss.hasPermi('member:member:add')")
    @Log(title = "会员管理", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody MallMemberAddBo bo) {
        return toAjax(iMallMemberService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改会员管理
     */
    @ApiOperation("修改会员管理")
    @PreAuthorize("@ss.hasPermi('member:member:edit')")
    @Log(title = "会员管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody MallMemberEditBo bo) {
        return toAjax(iMallMemberService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除会员管理
     */
    @ApiOperation("删除会员管理")
    @PreAuthorize("@ss.hasPermi('member:member:remove')")
    @Log(title = "会员管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iMallMemberService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
